﻿using System;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Text;
using BP.En;
using BP.Pub;
using BP.Sys;

namespace BP.DA
{
    /// <summary>
    /// 实体缓存
    /// </summary>
    public class Cache2024
    {
        #region 缓存的操作.
        /// <summary>
        /// 清除所有的实体缓存.
        /// </summary>
        public static void ClearCache()
        {
            _hts = new Hashtable();
        }
        #endregion 缓存的操作.

        #region 缓存ht
        private static Hashtable _hts;
        public static Hashtable hts
        {
            get
            {
                if (_hts == null)
                    _hts = new Hashtable();
                return _hts;
            }
        }
        #endregion

        #region 对实体的操作.
        /// <summary>
        /// 获得实体类
        /// </summary>
        /// <param name="enName">实体名字</param>
        /// <param name="pkVal">键</param>
        /// <returns>row</returns>
        public static DBSafes GetDBSafes(string tableName)
        {
            Boolean IsHave = false;
            foreach (String _key in hts.Keys) {
                if (_key == tableName) {
                    IsHave = true;
                    break;
                }
            }
            if (!IsHave)
            {
                DBSafes dBSafes = new DBSafes();
                dBSafes.Retrieve("TableName", tableName, "Idx");

                IsHave = false;
                foreach (String _key in hts.Keys)
                {
                    if (_key == tableName)
                    {
                        IsHave = true;
                        break;
                    }
                }
                if (!IsHave)
                {
                    hts.Add(tableName, dBSafes);
                }
                return dBSafes;
            }
            else {
                return hts[tableName] as DBSafes;
            }

            /*
            if (!hts.Contains(tableName))
            {
                DBSafes dBSafes = new DBSafes();
                dBSafes.Retrieve("TableName", tableName, "Idx");
                if (!hts.Contains(tableName))
                {
                    hts.Add(tableName, dBSafes);
                }   
                return dBSafes;
            }
            else {
                return hts[tableName] as DBSafes;
            }*/
            /* 检测异常调整
            if (hts.ContainsKey(tableName) == false)
            {
                DBSafes dBSafes = new DBSafes();
                dBSafes.Retrieve("TableName", tableName, "Idx");
                if (hts.ContainsKey(tableName) == false)
                    hts.Add(tableName, dBSafes);
                return dBSafes;
            }*/
        }
        #endregion 对实体的操作.

    }
}
